<template>
  <div class="app-container container">
    <el-form class="demo-form-inline" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch"
      label-width="90px">
      <el-form-item label="事务所名称" prop="officeId">
        <el-select v-model="queryParams.officeId" style="width: 100%;" placeholder="请选择">
          <el-option v-for="dict in sys_office" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="客户来源" prop="customerFrom">
        <el-input v-model="queryParams.customerFrom" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="报告种类" prop="typeId">
        <el-select v-model="queryParams.typeId" style="width: 100%;" placeholder="请选择">
          <el-option v-for="dict in reportTypes" :key="dict.typeId" :label="dict.joinTypeName"
            :value="dict.typeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目经理" prop="creater">
        <el-input v-model="queryParams.creater" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="收款单位" prop="payCompany">
        <el-input v-model="queryParams.payCompany" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="是否收款" prop="isPayment">
        <el-select v-model="queryParams.isPayment" style="width: 100%;" placeholder="请选择">
          <el-option :value="0" label="未收款"></el-option>
          <el-option :value="1" label="已收款"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否(3+2)" prop="istat">
        <el-select v-model="queryParams.istat" style="width: 100%;" placeholder="请选择">
          <el-option :value="1" label="是"></el-option>
          <el-option :value="0" label="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传状态" prop="isUpload">
        <el-select v-model="queryParams.isUpload" style="width: 100%;" placeholder="请选择">
          <el-option :value="0" label="未上传"></el-option>
          <el-option :value="1" label="已上传"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="签字人" prop="signUser">
        <el-input v-model="queryParams.signUser" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table class="table" v-loading="loading" :data="infoList">
      <el-table-column label="序号" type="index" fixed align="center" width="70" />
      <el-table-column label="报告编号" align="center" fixed prop="reportNo" width="120" />
      <el-table-column label="事务所名称" align="center" prop="officeName" width="120" />
      <el-table-column label="客户名称" align="center" prop="customerName" width="120" />
      <el-table-column label="客户来源" align="center" prop="customerFrom" width="120" />
      <el-table-column label="报告种类" align="center" prop="typeName" width="120" />
      <el-table-column label="助理名称" align="center" prop="assistant" width="120" />
      <el-table-column label="资产总计" align="center" prop="totalMoney" width="120" />
      <el-table-column label="收入总计" align="center" prop="incomeMoney" width="120" />
      <el-table-column label="收费金额" align="center" prop="payFee" width="120" />
      <el-table-column label="收款单位" align="center" prop="payCompany" width="120" />
      <el-table-column label="是否收款" align="center" prop="isPayment" width="120">
        <template #default="scope">
          {{ scope.row.isPayment === 1 ? '已收款' : '未收款' }}
        </template>
      </el-table-column>
      <el-table-column label="是否(3+2)" align="center" prop="istat" width="120">
        <template #default="scope">
          {{ scope.row.istat === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="签字人" align="center" prop="signUser" width="120" />
      <el-table-column label="项目经理" align="center" prop="creater" width="120" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="120" />
      <el-table-column label="关联报告" align="center" prop="relateReportId" width="120" />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" @click="handleUpload(scope.row)">上传</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改预警设置对话框 -->
    <el-dialog :title="title" v-model="open" width="650px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="客户来源" prop="customerFrom">
          <el-input v-model="form.customerFrom" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item v-if="!form.reportId" label="报告种类" prop="typeIds">
          <el-select style="width: 500px;" v-model="form.typeIds" multiple placeholder="请选择">
            <el-option v-for="dict in reportTypes" :key="dict.typeId" :label="dict.joinTypeName"
              :value="dict.typeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="报告种类">
          {{ form.typeName }}
        </el-form-item>
        <el-form-item label="助理名称" prop="assistant">
          <el-input v-model="form.assistant" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="资产总计" prop="totalMoney">
          <el-input v-model="form.totalMoney" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="收入总计" prop="incomeMoney">
          <el-input v-model="form.incomeMoney" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-select style="width: 500px;" v-model="form.year" placeholder="请选择">
            <el-option v-for="dict in yearList" :key="dict" :label="dict" :value="dict"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <UpFileDialog v-model:visible="showUpFile" :userInfo="currentItem" @finish="handleQuery"/>
  </div>
</template>

<script setup name="Info">
import { getUserList } from "@/api/relation";
import { getReportList, getReportManagerList, getYearList, addReport, updateReport } from "@/api/report";
import UpFileDialog from './upFileDialog'
const { proxy } = getCurrentInstance();
const { sys_office } = proxy.useDict('sys_office');
const userList = ref([])
const reportTypes = ref([])
const yearList = ref([])

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const showUpFile = ref(false)
const currentItem = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    customerName: [{ required: true, message: "请输入客户名称", trigger: "blur" }],
    customerFrom: [{ required: true, message: "请输入客户来源", trigger: "blur" }],
    totalMoney: [{ required: true, message: "请输入资产总计", trigger: "blur" }],
    incomeMoney: [{ required: true, message: "请输入收入总计", trigger: "blur" }],
    year: [{ required: true, message: "请选择年份", trigger: "change" }],
    typeIds: [{ required: true, message: "请选择报告种类", trigger: "change" }]
  }
});

const { queryParams, form, rules } = toRefs(data);
/** 查询预警设置列表 */
function getList() {
  loading.value = true;
  getReportManagerList(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(error => {
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加报告";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = JSON.parse(JSON.stringify(row))
  open.value = true;
  title.value = "修改报告";
}
/** 上传文件操作 */
function handleUpload(row) {
  currentItem.value = JSON.parse(JSON.stringify(row))
  showUpFile.value = true;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      console.log("🚀 ~ submitForm ~ form.value:", form.value)
      const p = JSON.parse(JSON.stringify(form.value))
      if (form.value.reportId != null) {
        updateReport(p).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReport(p).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 查询卡片管理列表 */
function getUsers() {
  getUserList().then(response => {
    userList.value = response.rows;
  });
}
function getReport() {
  getReportList().then(response => {
    console.log("🚀 ~ getReport ~ response:", response)
    reportTypes.value = response.data;
  });
}
function getYears() {
  getYearList().then(response => {
    console.log("🚀 ~ yearList ~ response:", response)
    yearList.value = response.data;
  });
}
getYears()
getReport()
getUsers()
getList();
</script>
<style lang="scss" scoped>
.demo-form-inline {
  :deep(.el-form-item__content) {
    width: 260px;
  }
}

.container {
  height: calc(100vh - 85px);
  // overflow: auto;
  display: flex;
  flex-direction: column;

  .table {
    flex: 1;
  }
}
</style>